/**
 * Created by Ji.wenjiang on 2016/9/8.
 */

angular.module('inspinia')
  .controller('openForm_new', function ($scope, $timeout, $log, values,users) {
    init();
    function init() {
      $scope.forms = values.formValues;
      $scope.formUrl = "/user/hospitalizedCard.html";
      $scope.printShow = true;
      $scope.treeConfig = {
        'plugins': ['types', 'dnd'],
        'types': {
          'default': {
            'icon': 'fa fa-folder'
          },
          'html': {
            'icon': 'fa fa-file-code-o'
          },
          'svg': {
            'icon': 'fa fa-file-picture-o'
          },
          'css': {
            'icon': 'fa fa-file-code-o'
          },
          'img': {
            'icon': 'fa fa-file-image-o'
          },
          'js': {
            'icon': 'fa fa-file-text-o'
          }

        }
      };
      $scope.JSON=[
        {
          "id": "ajson1",
          "parent": "#",
          "text": "表单组",
          "state": {
            "opened": true
          },
          "__uiNodeId": 1
        }, {
          "id": "ajson2",
          "parent": "ajson1",
          "text": "入院证",
          "state": {
            "opened": true
          },
          "__uiNodeId": 2
        }, {
          "id": "ajson3",
          "parent": "ajson2",
          "text": "2016-05-21 入院",
          "state": {
            "opened": true
          },
          "type": "css",
          "__uiNodeId": 3
        },
        {
          "id": "ajson4",
          "parent": "ajson2",
          "text": "2016-08-05 入院",
          "state": {
            "opened": true
          },
          "type": "css",
          "__uiNodeId": 3
        },
        /*{
         "id": "ajson4",
         "parent": "ajson2",
         "text": "bootstrap.css",
         "state": {
         "opened": true
         },
         "type": "css",
         "__uiNodeId": 4
         },
         {
         "id": "ajson5",
         "parent": "ajson2",
         "text": "style.css",
         "state": {
         "opened": true
         },
         "type": "css",
         "__uiNodeId": 5
         },*/
        {
          "id": "ajson6",
          "parent": "ajson1",
          "text": "处方笺",
          "state": {
            "opened": true
          },
          "__uiNodeId": 6
        },
        {
          "id": "ajson9",
          "parent": "ajson6",
          "text": "2015-05-05 处方笺",
          "state": {
            "opened": true
          },
          "type": "img",
          "__uiNodeId": 9
        },
        {
          "id": "ajson10",
          "parent": "ajson6",
          "text": "2015-05-04 处方笺",
          "state": {
            "opened": true
          },
          "type": "svg",
          "__uiNodeId": 10
        },
        {
          "id": "ajson11",
          "parent": "ajson6",
          "text": "2016-12-11 处方笺",
          "state": {
            "opened": true
          },
          "type": "img",
          "__uiNodeId": 11
        },


        {
          "id": "ajson7",
          "parent": "ajson1",
          "text": "检验报告单",
          "state": {
            "opened": true
          },
          "__uiNodeId": 7
        },
        {
          "id": "ajson28",
          "parent": "ajson7",
          "text": "2016-11-11 检验报告",
          "state": {
            "opened": true
          },
          "type": "img",
          "__uiNodeId": 28
        },
        {
          "id": "ajson8",
          "parent": "ajson1",
          "text": "验证单",
          "state": {
            "opened": true
          },
          "__uiNodeId": 8
        },
        {
          "id": "ajson44",
          "parent": "ajson1",
          "text": "放射治疗记录单",
          "state": {
            "opened": true
          },
          "__uiNodeId": 44
        },

      ];
      $scope.treeData = $scope.JSON;
      // $('#tree').jstree({ 'core' : {
      //   'data' : [
      //     { "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
      //     { "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
      //     { "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
      //     { "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
      //   ]
      $scope.treeEventsObj = {
        'ready': readyCB,
        'create_node': createNodeCB
      }

      function readyCB() {
        $log.info('ready called');
      };

      function createNodeCB(e,item) {
        $log.info('create_node called');
      };
    }
    // $("#tree").bind('click',function () {
    //   $log.debug(event.target);
    //   switch (event.target.id){
    //     case "ajson3_anchor":hospitalizedCard();
    //           break;
    //     case "ajson11_anchor":makingModel();
    //           break;
    //     default:
    //           break;
    //   }
    //   // console.log(123);
    //   function hospitalizedCard() {
    //     $scope.formUrl = "/user/hospitalizedCard.html";
    //     $scope.user=users[0];
    //     $scope.$apply();
    //   }
    //   function makingModel() {
    //     $scope.formUrl = "/makingModel/modelForm.html";
    //     $scope.user=users[0];
    //     $scope.$apply();
    //   }
    // })

    $('#tree').bind("activate_node.jstree", function (obj, e) {
      // 处理代码
      // 获取当前节点
      $log.debug(e);
      switch (e.node.id){
            case "ajson3":hospitalizedCard();
                  break;
            case "ajson11":makingModel();
                  break;
            default:
                  break;
          }
          // console.log(123);
          function hospitalizedCard() {
            $scope.formUrl = "/user/hospitalizedCard.html";
            $scope.user=users[0];
            $scope.$apply();
          }
          function makingModel() {
            $scope.formUrl = "/makingModel/modelForm.html";
            $scope.user=users[0];
            $scope.$apply();
          }

    });


    // $('#tree').jstree(true).select_node('child_node_1');

    $scope.print = function () {
      window.print();
    }

  })
